﻿using System;
using System.Collections.Generic;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.ScreenManagerLib.Widget;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Epitech.Krisk.Xna.UI.Game.Menu;
using Epitech.Krisk.Xna.UI.Game.InGame.Gameplay;

namespace Epitech.Krisk.Xna.UI.Game.Utils
{
    public class Pause : MenuScreen
    {
        private Texture2D _popup;
        private Texture2D _blank;
        private Vector2 _pos;
        private LayoutEntry _button;
        private GamePlay _gamePlay;

        public Pause(GamePlay gamePlay)
            : base("")
        {
            _gamePlay = gamePlay;
        }

        public override void LoadContent()
        {
            _popup = ScreenManager.Game.Content.Load<Texture2D>("Utils/Popup");
            _blank = ScreenManager.Game.Content.Load<Texture2D>("blank");
            MenuEntry _reprendre = new MenuEntry("[ REPRENDRE ]");
            MenuEntry _save = new MenuEntry("[ SAUVEGARDER ]");
            MenuEntry _quit = new MenuEntry("[ QUITTER ]");
            _reprendre.Selected += Reprendre;
            _save.Selected += Save;
            _quit.Selected += Quit;


            _pos = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2, ScreenManager.GraphicsDevice.Viewport.Height / 2);
            _pos.X -= _popup.Width / 2;
            _pos.Y -= _popup.Height / 2;


            _button =new LayoutEntry(new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2, _pos.Y + _popup.Height - 250)) { Alignement = LayoutEntry.TextAlignement.Center };
            _button.AddEntry(_reprendre);
            _button.AddEntry(_save);
            _button.AddEntry(_quit);

            foreach (MenuEntry item in _button.MenuEntries)
            {
                item.FontSize = 1f;
                item.SelectedColor = new Color(119, 121, 81);
                item.DefaultColor = new Color(208, 44, 6);
                item.IsPulse = false;
            }

            MenuEntries.Add(_button);
            base.LoadContent();
        }

        private void OkSelected(object sender, PlayerIndexEventArgs e)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
        }

        public override void Draw(GameTime gameTime)
        {
            Rectangle area = ScreenManager.GraphicsDevice.Viewport.Bounds;


            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.Draw(_blank, area, Color.FromNonPremultiplied(0, 0, 0, 128));
            ScreenManager.SpriteBatch.Draw(_popup, _pos, Color.White);

            ScreenManager.SpriteBatch.End();
            base.Draw(gameTime);
        }

        #region Handle Input

        protected void Reprendre(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
        }

        protected void Save(object sender, PlayerIndexEventArgs playerIndex)
        {
            // open save screen
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new SaveView(_gamePlay), null);
        }

        protected void Quit(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.RemoveLastScreen();
            ScreenManager.AddScreen(new EndGameView(_gamePlay), null);
        }

        #endregion

    }

}
